import db from "../models/index.js";
import {Op} from "sequelize";
import {clearObject, getNow} from "../utils/utils.js";

let gupiao = db.gupiao;
let gupiaoDao = {};

// 获取用户设置的股票数据
gupiaoDao.getUserGuPiaoAll = function (user_id) {
  return gupiao.findAndCountAll({
    where: {
      user_id
    }, raw: true
  });
};

// 获取用户该股票的设置数据
gupiaoDao.getUserGuPiao = function (user_id, gupiao_id) {
  return gupiao.findOne({
    where: {
      user_id, gupiao_id
    },
    raw: true
  });
};

// 获取该股票的用户数据
gupiaoDao.getGuPiaoAll = function (gupiao_id) {
  return gupiao.findAndCountAll({
    where: {
      gupiao_id
    },
    raw: true
  });
};

// 获取该股票的买入用户数据
gupiaoDao.getGuPiaoAllWithDoit = function (gupiao_id) {
  return gupiao.findAndCountAll({
    where: {
      gupiao_id,
      do_it: 1
    },
    raw: true
  });
};

// 获取该股票的未买入用户数据
gupiaoDao.getGuPiaoAllWithoutDoit = function (gupiao_id) {
  return gupiao.findAndCountAll({
    where: {
      gupiao_id,
      do_it: 0
    },
    raw: true
  });
};

// 获取是否还有该股票id的数据
gupiaoDao.getUserGuPiaoCount = function (gupiao_id, t) {
  if (!t) {
    return gupiao.count({
      where: {
        gupiao_id
      }
    });
  }
  return gupiao.count({
    where: {
      gupiao_id
    },
    transaction: t
  });
};

// 用户添加新的股票数据
gupiaoDao.addNewGuPiao = function (user_id, gupiao_id, price_buy, price_sell_high, price_sell_low, t) {
  let now = getNow();
  if (!t) {
    return gupiao.create({
      user_id, gupiao_id, price_buy, price_sell_high, price_sell_low, create_time: now, update_time: now, do_it: 0
    });
  }
  return gupiao.create({
    user_id, gupiao_id, price_buy, price_sell_high, price_sell_low, create_time: now, update_time: now, do_it: 0
  }, {transaction: t});
};

// 用户修改股票的数据
gupiaoDao.updateGuPiao = function (user_id, gupiao_id, price_buy, price_sell_high, price_sell_low, do_it, t) {
  // 清除对象中的空值
  let obj = clearObject(
    {price_buy, price_sell_high, price_sell_low, do_it, update_time: getNow()}
  );
  if (!t) {
    return gupiao.update(obj, {where: {user_id, gupiao_id}});
  }
  return gupiao.update(obj, {
    where: {
      user_id, gupiao_id
    }, transaction: t
  });
};

// 用户删除股票的数据
gupiaoDao.deleteGuPiao = function (user_id, gupiao_id, t) {
  if (!t) {
    return gupiao.destroy({where: {user_id, gupiao_id}});
  }
  return gupiao.destroy({where: {user_id, gupiao_id}, transaction: t});
};

// 获取买入且价格在上下限之外的用户id 也就是该卖的用户id
gupiaoDao.getSellUser = function (gupiao_id, price) {
  return gupiao.findAndCountAll({
    attributes: ["user_id"],
    where:{
      do_it: 1,
      price_sell_high: {
        [Op.lte]: price
      },
      price_sell_low: {
        [Op.gte]: price
      },
      gupiao_id
    },
    raw: true
  });
};

// 获取未买入且价格在买入价格之下的用户id 也就是该买入的用户id
gupiaoDao.getBuyUser = function (gupiao_id, price) {
  return gupiao.findAndCountAll({
    attributes: ["user_id"],
    where:{
      do_it: 0,
      price_buy: {
        [Op.gte]: price
      },
      gupiao_id
    },
    raw: true
  });
};

export default gupiaoDao;
